#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <objbase.h>
using namespace std;

const char* getFileName(char driveLetter) {
	GUID guid;
	HRESULT hResult = CoCreateGuid(&guid);
	if (hResult != S_OK)
		return nullptr;
	const int buf_size = 128;
	char* buf = new char[buf_size];
	sprintf_s(buf, buf_size,
		"%c:\\write-file-autorefresh-{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}.txt",
		driveLetter,
		guid.Data1, guid.Data2, guid.Data3,
		guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
		guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
	return buf;
}

const char* getFileName() {
	return getFileName('Z');
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
	const char* filename;
	if (szCmdLine != NULL &&
		(szCmdLine[0] >= 'A' && szCmdLine[0] <= 'Z'
			|| szCmdLine[0] >= 'a' && szCmdLine[0] <= 'z')) {
		filename = getFileName(szCmdLine[0]);
	} else {
		filename = getFileName();
	}
	if (filename == nullptr) {
#if _DEBUG
		MessageBox(NULL, L"GUID generate error", L"error", MB_OK);
#endif
		return 0;
	}
	FILE* file = fopen(filename, "w");
	if (file != NULL) {
		fprintf(file, "This is auto refresh program write file.\nCurrent filename: %s", filename);
		fclose(file);
		Sleep(1000);
		remove(filename);
	} else {
#if _DEBUG
		MessageBox(NULL, L"Open file error", L"error", MB_OK);
#endif
	}
	return 0;
}